<link type="text/css" href="/admins/js/pagerView/pager-view.css" rel="stylesheet" />
<link type="text/css" href="/admins/js/jquery/css/ui-lightness/jquery-ui-1.8.20.custom.css" rel="stylesheet" />
<script type="text/javascript" src="/admins/js/jquery/jquery-ui-1.8.20.custom.min.js"></script>
<script type="text/javascript" src="/admins/js/jquery/jquery.ui.datepicker-zh-CN.js"></script>
<script type="text/javascript" src="/admins/js/pagerView/pager-view.js"></script>
<script type="text/javascript">
    $(function(){
    
        $.datepicker.setDefaults( $.datepicker.regional[ "zh-CN" ] );
        $( "#billboy #startTime" ).datepicker({
            changeMonth: true,
            changeYear: true
        });
        $( "#billboy #dateExpired" ).datepicker({
            changeMonth: true,
            changeYear: true
        });
    });
</script>
<style>
    #billboy{
        font-size: 14px;
        width: 80%;
    }
    #userdetail{
        line-height: 36px;
        padding-left: 20px;
        padding-bottom: 10px;
        font-size: 16px;
    }
    #userdetail input{
        width: 80%;
        height: 20px;
        font-size: 16px;
    }
    #bf{
        background-color: #eee;
        text-align: right;
        width: 15%;
        height: 50px;
    }
    #cf{
        width: 35%;
    }
    select{
        font-size: 16px;
        height:24px;
    }
</style>
<?php
if($this->ma == 1){
    echo '<script language="javascript" type="text/javascript">alert("优惠券添加成功，批次号为：'.$this->couponNo.'！");location.href = "/admini/manageuser/coupon/"; </script>';
}else if($this->ma == 3){
    echo '<script language="javascript" type="text/javascript">alert("用户不能为空、优惠券序号不能为空、折扣不能为空和零！");</script>';
}else if($this->ma == 22){
    echo '<script language="javascript" type="text/javascript">alert("该优惠券序号已存在！");</script>';
}
$arrup = $this->arrup;
?>

<div id="billboy">
    <div id='userdetail'>
        添加优惠券
        <form action="/admini/manageuser/addcoupon/" method="post">
        <table width="100%" border="1" cellpadding="0" cellspacing="0">  
            <tr>
              <td id='bf'>所属用户：</td>
              <td id='cf' valign="middle">&nbsp;&nbsp;<input type="text" name="username" id="username" value="<?php echo $arrup["username"]?$arrup["username"]:"";?>"/></td>
              <td id='bf'>仓库号：</td>
              <td id='cf' valign="middle">&nbsp;&nbsp;<input type="text" name="storeno" id="storeno"  value="<?php echo $arrup["storeno"]?$arrup["storeno"]:"";?>"/></td>
            </tr>
            <tr>
                <td id='bf'>批次序号：</td>
                <td id='cf' valign="middle">&nbsp;&nbsp;<select id="batchNo" name="batchNo" >
                      <?php  $arrfirst = array();
                            foreach ($this->batch as $key => $batchval) { 
                            if($key == 0){$arrfirst = $batchval;}
                          ?>                        
                          <option value="<?php echo $batchval->no;?>" <?php echo $arrup["batchNo"]?($arrup["batchNo"]==$batchval->no?"selected":""):""?>><?php echo $batchval->name;  ?></option>
                      <?php }?>
                  </select></td>
              <td id='bf'>使用范围：</td>
              <td id='cf' valign="middle" >&nbsp;&nbsp;<select id="scope" name="scope" >
                      <?php foreach ($this->arrscope as $key => $scopeval) { ?>
                          <option value="<?php echo $key;?>" <?php echo $arrup["scope"]?($arrup["scope"]==$key?"selected":""):""?>><?php echo $scopeval;?></option>
                      <?php }?>
                  </select></td>
            </tr>
            <tr>
                <td id='bf'>优惠券名称：</td>
                <td id='cf' valign="middle">&nbsp;&nbsp;<input type="text" name="name" id="name" value="<?php echo $arrup["name"]?$arrup["name"]:($arrfirst->name?$arrfirst->name:"");?>"/></td>
                <td id='bf'>优惠券类型：</td>
                <td id='cf' valign="middle">&nbsp;&nbsp;<select id="batchType" name="batchType" >
                      <?php  foreach ($this->couponTypeAll as $key => $typeVal) {  ?>                        
                          <option value="<?php echo $key;?>" <?php echo $arrfirst->type==$key?"selected":($arrup["couponType"]==$key?"selected":"")?>><?php echo $typeVal;  ?></option>
                      <?php }?>
                  </select></td>
            </tr>
            <tr>
                <td id='bf'>底线金额：</td>
                <td id='cf' valign="middle">&nbsp;&nbsp;<input type="text" name="money" id="money" value="<?php echo $arrup["money"]?$arrup["money"]:($arrfirst->money?$arrfirst->money:0);?>"/></td>
                <td id='bf'>折扣：</td>
                <td id='cf' valign="middle">&nbsp;&nbsp;<input type="text" name="discount" id="discount"  value="<?php echo $arrup["discount"]?$arrup["discount"]:($arrfirst->discount?$arrfirst->discount:"");?>"/></td>
            </tr>
            <tr>
                <td id='bf'>开始时间：</td>
                <td id='cf' valign="middle">&nbsp;&nbsp;<input type="text" name="startTime" id="startTime" value="<?php echo $arrup["startTime"]?$arrup["startTime"]:($arrfirst->dateStart?$arrfirst->dateStart:"");?>"/></td>
                <td id='bf'>结束时间：</td>
                <td id='cf' valign="middle">&nbsp;&nbsp;<input type="text" name="dateExpired" id="dateExpired"  value="<?php echo $arrup["dateExpired"]?$arrup["dateExpired"]:($arrfirst->dateEnd?$arrfirst->dateEnd:"");?>"/></td>
            </tr>
            <tr>
              <td id='bf'>备注：</td>
              <td id='cf' valign="middle" colspan="3">&nbsp;&nbsp;<textarea name="common" id="common"  cols="99" rows="12" style="font-size:16px;resize:none;"><?php echo $arrup["common"]?$arrup["common"]:""?></textarea></td>
            </tr>
            <tr>
                <td id='cf' colspan="4" align="right"><input type="hidden" name="userId" id="userId" value="<?php echo $arrup["userId"]?$arrup["userId"]:"";?>" />
                    <input type="hidden" name="operate" id="operate" value="add" />
                    <input type="submit" id="submit" name="submit" value="添加优惠券" style="background-color: #FF8312;width: 100px;height: 36px;color: white;font-size: 16px;border-radius:8px;" />&nbsp;&nbsp;
                    <input type="button" id="rehref" name="rehref" value="返回" style="background-color: #1668A4;width: 100px;height: 36px;color: white;font-size: 16px;border-radius:8px;" /></td>
            </tr>
          </table>
          </form>
    </div>
</div>
<script type="text/javascript">
    var isuser = true;
    $('#username').blur(function(){
        var username = $(this).val();
        if(username != ''){
            $.post('/admini/manageuser/bystoreno', {'username':username}, function(data){
                if(data.id != ""){
                    $('#storeno').attr('value',data.storeno);
                    $('#userId').attr('value',data.id);
                    isuser = true;
                }else{
                    isuser = false;
                    $('#storeno').attr('value','');
                    $('#userId').attr('value','');
                    
                }
            }, 'json');
        }else{
            $('#storeno').attr('value','');
            $('#userId').attr('value','');
        }
    });
    $('#storeno').blur(function(){
        var storeno = $(this).val();
        if(storeno != ''){
            $.post('/admini/manageuser/byusername', {'storeno':storeno}, function(data){
                if(data.id != ""){
                    $('#username').attr('value',data.username);
                    $('#userId').attr('value',data.id);
                    isuser = true;
                }else{
                    alert("没有该用户，请重新输入所属用户！");
                    isuser = false;
                    $('#username').attr('value','');
                    $('#userId').attr('value','');
                }
            }, 'json');
        }else{
            $('#username').attr('value','');
            $('#userId').attr('value','');
        }
    });
    $('#no').blur(function(){
        var no = $(this).val();
        if(no != ''){
            $.post('/admini/manageuser/bycouponno', {'no':no}, function(data){
                if(data == 2){
                    alert("该优惠券序号已存在！");
                    $("#no").val("");
                }
            }, 'json');
        }
    });
    $("#batchNo").change(function(){
        var no = $(this).val();
        if(no != ''){
            $.post('/admini/manageuser/bybatchno', {'no':no}, function(data){
                if(data.sjtype == 2){
                    var money = data.money?data.money:0;
                    $("#name").val(data.name);
                    $("#money").val(money);
                    $("#discount").val(data.discount);
                    $("#startTime").val(data.dateStart);
                    $("#dateExpired").val(data.dateEnd);
                    $("#ckmoney").text(money);
                    $("#ckdiscount").text(data.discount);
                    $("#ckstartTime").text(data.dateStart);
                    $("#ckdateExpired").text(data.dateEnd);
                    $("#ckmaxCount").text(data.maxCount);
                    $("#ckgiveCount").text(data.giveCount);
                }
            }, 'json');
        }
    });
    $("#rehref").click(function(){
        history.back();
    });
    $('#submit').click(function(){
        var storeno = $('#storeno').val();
        var startTime = $("#startTime").val();
        var dateExpired = $("#dateExpired").val();
        var discount = $("#discount").val();
        var no = $("#no").val();
        if(!isuser){
            alert("没有该用户，请重新输入所属用户！");
            return false;
        }else if(storeno == ''){
            alert("没有该用户，请重新输入所属用户！");
            return false;
        }else if(no == ''){
            alert("优惠券序号不能为空！");
            return false;
        }else if(startTime == ''){
            alert("开始时间不能为空！");
            return false;
        }else if(dateExpired == ''){
            alert("结束时间不能为空！");
            return false;
        }else if(startTime>dateExpired){
            alert("开始时间不能大于结束时间！");
            return false;
        }else if(discount == '' || discount == 0){
            alert("优惠券的折扣不能为空或为零！");
            return false;
        }else{
            return true;
        }
    });
</script>